#include"core_conf.h"
#include<stdio.h>
#include<stdlib.h>
#include"core_conf_map.h"
#include"mail.h"

static int db_count = 0;

//the pcap_mode 1 represent use of interface and the 0 represent use  of file
//the dafault interface is eth0
int value_to_index(char *value, char (*map)[64], int n)
{
	if (value == NULL || map == NULL) {
		return -1;
	}

	int i=0;

	for (i = 0; i < n; i++) {
		if (strcmp(value, map[i]) == 0) {
			return i;
		}
	}
	return -1;
}

//this is the main core conf init from file 
void core_conf_from_file(struct core_conf *Core_conf,char * file_name)
{
	log_info("begin get_config_from_file.");
	#define MAX_LINE 1024
	char buf[MAX_LINE];
	FILE *fp;
	
	if ((fp = fopen(file_name, "r")) == NULL) {
		log_warning("cann't open core config file, use default config.");
		return ;
	}


	while (fgets(buf, MAX_LINE, fp) != NULL) {
		char lvalue[256] = {'\0'};		
		char rvalue[256] = {'\0'};

		if (buf[0] == '#' || buf[0] == '\n') {
			continue;
		}

		sscanf(buf, "%s = %s\n", lvalue, rvalue);

		if (rvalue == NULL || lvalue == NULL || strcmp(rvalue, "\0") == 0 || strcmp(rvalue, " ") ==  0) {
			continue;
		}
		
		int lvalue_index = -1;
		lvalue_index = value_to_index(lvalue, core_conf_map, END_OF_CORE_CONF_INDEX);
		log_info("lvalue_index = %d", lvalue_index);

		switch(lvalue_index)
		{
			case DB_CONF:
				{
					if(db_count < DB_SUPPORT_MAX_NUM) {
					strncpy(Core_conf->db_config[db_count], rvalue, DB_CONFIGURE_MAX_LEN);
					Core_conf->db_config[db_count][DB_CONFIGURE_MAX_LEN - 1] = '\0';
					db_count++;
					Core_conf->db_count++;
					break;	
					}	
				}
			case PCAP_MODE:
				{
					int pm = -1;
					pm = atoi(rvalue);		
					Core_conf->pcap_mode = pm;
					break;
				}
			case MODE:
				{
					int fm = -1;
					fm = atoi(rvalue);		
					Core_conf->mode = fm;
					break;
				}
			case PCAP_FILE:
				{
					char *pf=(char *)malloc(sizeof(char) *128);
					if(pf==NULL)
					{
						log_error("no enough mem");
						exit(-1);
					}
					strcpy(pf,rvalue);
					Core_conf->pcap_file=pf;
					break;
				}
			case PCAP_INTERFACE:
				{
					char *pi = (char *)malloc(sizeof(char) * 128);
					if (pi == NULL) {
						log_error("no enough mem.");
						exit(-1);
					}
					strcpy(pi, rvalue);
					Core_conf->pcap_interface = pi;
					break;
				}
			case TO:
				{
					char *to = (char *)malloc(MAIL_TO_MAX_LEN);
					if (to == NULL) {
						log_error("no enough mem.");
						exit(-1);
					}
					strcpy(to, rvalue);
					Core_conf->to = to;
					break;
				}
			case USLEEP_TIME:
				{
					int ut = -1;
					ut = atoi(rvalue);		
					Core_conf->usleep_time = ut;
					break;
				}
			case USE_MM:
				{
					int um = -1;
					um = atoi(rvalue);		
					Core_conf->use_mm = um;
					break;
				}
			case MM_LIMIT:
				{
					unsigned long ml;
					ml = atol(rvalue);
					Core_conf->mm_limit = ml;
					break;
				}
			case MAX_ID:
				{
					unsigned long mi;
					mi = atol(rvalue);
					Core_conf->max_id = mi;
					break;
				}
			case USE_MAIL:
				{
					unsigned long um;
					um = atoi(rvalue);
					Core_conf->use_mail = um;
					break;
				}
	
			case NODE_FILE:
				{
					char *pf=(char *)malloc(sizeof(char) *128);
                                        
					if(pf==NULL)
                                        {
                                                log_error("no enough mem");
                                                exit(-1);
                                        }

                                        strcpy(pf,rvalue);
                                        Core_conf->node_file=pf;
                                        break;
				}
			case SERVER_FILE:
				{
					char *pf=(char *)malloc(sizeof(char) *128);
                                        
					if(pf==NULL)
                                        {
                                                log_error("no enough mem");
                                                exit(-1);
                                        }

                                        strcpy(pf,rvalue);
                                        Core_conf->server_file=pf;
                                        break;
				}
			case BALANCE_FILE:
				{
					char *pf=(char *)malloc(sizeof(char) *128);
                                        
					if(pf==NULL)
                                        {
                                                log_error("no enough mem");
                                                exit(-1);
                                        }

                                        strcpy(pf,rvalue);
                                        Core_conf->balance_file=pf;
                                        break;
				}
				default:
					break;
		}	
	}	
	
}
//this is the socket of client init from file

void core_conf_init_node(struct core_conf *Core_conf,char * file_name)
{
	FILE *fp;		
	if((fp=fopen(file_name,"r"))==NULL)
        {
                log_info("can not open this file");
                return;
        }
	
	//this is get the node num	
	char node_temp[20];
	fgets(node_temp,19,fp);
	int node_num=atoi(node_temp);
	Core_conf->Node_conf.node_num=node_num;

	//next is getting node_num node conf
	int k=0;

	for(k=0;k<node_num;k++)
	{
		char a[3][20];
		char b[3][20];
		
	        fgets(a[0],19,fp);
		fgets(a[1],19,fp);
		fgets(a[2],19,fp);

        	int i=0;
        	int j=0;
		int m=0;

		for(m=0;m<3;m++)
		{
        		for(i=0;i<strlen(a[m]);i++)
        		{
                		if(a[m][i]==':')
                		{
                        		for(j=0;j<strlen(a[m])-i;j++)
                        		{
                        	        	b[m][j]=a[m][j+i+1];
                       			}
               			}
        		}
		}

        	strcpy(Core_conf->Node_conf.Node[k].hostname,b[0]);
        	Core_conf->Node_conf.Node[k].port=atoi(b[1]);
        	Core_conf->Node_conf.Node[k].ip.s_addr=inet_addr(b[2]);
	}
}

void core_conf_init_server(struct node *Node,char *file_name)
{
	FILE *fp;
		
	if((fp=fopen(file_name,"r"))==NULL)
        {
                log_info("can not open this file");
                return;
        }
	
	//next is getting node_num node conf

	char a[3][30];
	char b[3][30];
		
	fgets(a[0],29,fp);
	fgets(a[1],29,fp);
	fgets(a[2],29,fp);

        int i=0,j=0,m=0;

	for(m=0;m<3;m++)
	{
        	for(i=0;i<strlen(a[m]);i++)
        	{
               		if(a[m][i]==':')
               		{
                       		for(j=0;j<strlen(a[m])-i;j++)
                       		{
                       	        	b[m][j]=a[m][j+i+1];
                     		}
             		}
        	}
	}

        strcpy(Node->hostname,b[0]);
        Node->port=atoi(b[1]);
        Node->ip.s_addr=inet_addr(b[2]);	
}
